<?php


namespace app\api\model;



class SubjectAnalog extends BaseModel
{
    protected $pk = 'analog_id';

    protected $autoWriteTimestamp = true;

    protected function getTimeAttr($value){
        return date("i:s",$value);
    }

    public function user(){
        return $this->belongsTo('User','user_id','id')->field('id,nickname,avatar_url');
    }

    /**
     * 添加考试记录
     * @param $data
     * @return false|int
     */
    public  function insertLog($data){
            return self::save($data);
    }

    /**
     * 获取考试记录
     * @param $uid
     * @param $course
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLog($uid,$course,$car_status){
        return self::where('user_id','=',$uid)
            ->where('course','=',$course)
            ->where('car_status','=',$car_status)
            ->order("create_time desc")
            ->select();
    }

    /**
     * 获取用户最好成绩
     * @param $uid
     * @param $course
     * @param $car_status
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getUserLog($uid,$course,$car_status){
        return self::where('user_id', '=', $uid)
            ->where('course', '=', $course)
            ->where('car_status', '=', $car_status)
            ->field('user_id,time,max(record) record')
            ->with(['user'])
            ->find();
    }

    /**
     * 获取用户排行
     * @param $t_id
     * @param $course
     * @param $car_status
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getLogByTid($t_id,$course,$car_status){
        return self::where('t_id', '=', $t_id)
            ->where('course', '=', $course)
            ->where('car_status', '=', $car_status)
            ->field('user_id,time,max(record) record')
            ->group('user_id')
            ->order(['record'=>'desc','time'=>'asc'])
            ->limit('0',100)
            ->with(['user'])
            ->select();
    }





}